package com.pine.dbant.plugin.sqlite;

import com.pine.dbant.common.exeception.BusinessException;
import com.pine.dbant.common.model.ConnectionInfo;
import com.pine.dbant.common.model.JdbcMetadataTreeView;
import com.pine.dbant.common.plugin.AbstractJdbcDatabasePlugin;
import com.pine.dbant.common.plugin.Connector;
import com.pine.dbant.common.plugin.JdbcException;
import com.pine.dbant.common.plugin.PluginType;
import com.pine.dbant.common.plugin.annotion.Plugin;
import com.pine.dbant.common.plugin.meta.FunctionRegister;
import com.pine.dbant.common.store.CacheStore;
import com.pine.dbant.plugin.sqlite.function.*;
import com.pine.sunflower.core.plugin.Configuration;
import com.pine.sunflower.core.utils.Strings;
import org.apache.commons.lang3.StringUtils;

import javax.sql.DataSource;
import java.io.File;
import java.sql.SQLException;
import java.util.Arrays;


/**
 * @author xiaoyuan
 * @create 2022/11/8 16:06
 **/
@Plugin(id = "sqlite", type = PluginType.DATABASE)
public class SqliteDatabasePlugin extends AbstractJdbcDatabasePlugin {

    private static final String DB_FILE_PATH = "filePath";

    public SqliteDatabasePlugin(Configuration configuration, CacheStore cacheStore) {
        super(configuration,cacheStore);
    }

    @Override
    public String concatJdbcUrl(ConnectionInfo connectionInfo) {
        String dbFilePath = connectionInfo.getProperties().get(DB_FILE_PATH);
        if(StringUtils.isEmpty(dbFilePath)){
             throw new BusinessException("数据库文件地址不能为空");
        }
        File file = new File(dbFilePath);
        if(!file.exists()){
            throw new BusinessException(Strings.format("数据库文件[{}]不存在",dbFilePath));
        }
        return Strings.format("jdbc:sqlite:{}", file.getAbsolutePath());
    }

    @Override
    public FunctionRegister functionRegister() {
        return config -> config.addFunction(Arrays.asList(
                new SqliteCatalogFunction(JdbcMetadataTreeView.CONN.getType()),
                new SqliteTablesFunction(JdbcMetadataTreeView.TABLE_TYPE.getType()),
                new SqliteCatalogChildrenFunction(JdbcMetadataTreeView.CATALOG.getType()),
                new SqliteViewsFunction(JdbcMetadataTreeView.VIEW_TYPE.getType()),
                new SqliteTriggersFunction(JdbcMetadataTreeView.TRIGGER_TYPE.getType()),
                new SqliteAllMetadataEditorKeyFunction("allMetadataEditorKey"),
                new SqliteColumnsEditorKeyFunction("columnsEditorKey")));
    }

    @Override
    public String getDriver(ConnectionInfo connectionInfo) {
        return "org.sqlite.JDBC";
    }

    @Override
    public Connector createConnection(ConnectionInfo connectionInfo) {
        DataSource dataSource = getDataSource(connectionInfo);
        if (dataSource == null) {
            throw new BusinessException("获取数据库连接异常");
        }
        try {
            return new SqliteConnector(this.configuration, dataSource.getConnection(), connectionInfo);
        } catch (SQLException e) {
            throw new JdbcException("创建连接器失败", e);
        }
    }
}
